<template>
  <div>
    <van-nav-bar title="新增地址" left-text="返回" left-arrow @click-left="backhome"/>
    <van-address-edit
      :area-list="areaList"
      :address-info = "AddressInfo"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
    />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import { saveAction , detailAction} from "@/api/cart/order/address/addressDetail/index"
export default {
  name: "WorkspaceJsonIndex",

  data() {
    return {
        AddressInfo:{},
        areaList,
        searchResult: [],
        defaultProvince:"",
        defaultCity:"",
        defaultArea:"",
        provinceCode:"",
        cityCode:"",
        areaCode:""
    };
  },

  created(){
    var info = this.$route.params.id
    detailAction({
      id:info.id
    }).then(res=>{
      var  arr = res.data.address.split(' ');
      this.AddressInfo = {
        name: res.data.name,
        tel: res.data.mobile, 
        addressDetail: res.data.address_detail, 
        isDefault: res.data.isDefault, 
      };
      this.defaultProvince = arr[0]
      this.defaultCity = arr[1]
      this.defaultArea = arr[2]

      let resultProvince = Object.entries(areaList.province_list);
      // console.log(resultProvince)
      // console.log(arr);
      resultProvince.forEach((item) => {
        if (item[1] == this.defaultProvince) {
          this.provinceCode = item[0];
        }
      });

      let resultCity = Object.entries(areaList.city_list);
      resultCity.forEach((item) => {
        if (item[1] == this.defaultCity) {
          this.cityCode = item[0];
        }
      });

      let resultArea = Object.entries(areaList.county_list);
      resultArea.forEach((item) => {
        if (item[1] == this.defaultArea) {
          this.areaCode = item[0];
        }
      });
      console.log(this.provinceCode,this.cityCode,this.areaCode);

      this.AddressInfo.areaCode = this.provinceCode;
      this.AddressInfo.areaCode = this.cityCode;
      this.AddressInfo.areaCode = this.areaCode;

    })
  },

  mounted() {},

  methods: {
    onSave(content){
      if(this.$route.params.id){
        var info = this.$route.params.id
        var addressText = content.province+ " " + content.city + " " + content.county;
        console.log(content.isDefault);
        saveAction({
          address:addressText,
          addressId:info.id,
          checked:content.isDefault,
          detailadress:content.addressDetail,
          openId:localStorage.getItem('openId'),
          telNumber:content.tel,
          userName:content.name
        }).then(res=>{
          console.log(res);
        })
      }else{
        var addressText = content.province+ " " + content.city + " " + content.county;
        saveAction({
          address:addressText,
          addressId:'',
          checked:content.isDefault,
          detailadress:content.addressDetail,
          openId:localStorage.getItem('openId'),
          telNumber:content.tel,
          userName:content.name
        }).then(res=>{
          console.log(res);
        })
      }
      this.$router.push('/cart/order/address')
    },
    backhome(){
      console.log(111);
        this.$router.go(-1)
    }
  },
};
</script>

<style scoped>
</style>